/**
 * Copyright 2012-2013 University Of Southern California
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.workflowsim.examples.scheduling;

import java.io.File;
import java.util.Calendar;
import java.util.List;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.core.CloudSim;
import org.workflowsim.CondorVM;
import org.workflowsim.WorkflowDatacenter;
import org.workflowsim.Job;
import org.workflowsim.WorkflowEngine;
import org.workflowsim.WorkflowPlanner;
import org.workflowsim.utils.ClusteringParameters;
import org.workflowsim.utils.OverheadParameters;
import org.workflowsim.utils.Parameters;
import org.workflowsim.utils.ReplicaCatalog;

/**
 * This FCFS Scheduling Algorithm
 * 这是FCFS调度算法
 * @author Weiwei Chen
 * @since WorkflowSim Toolkit 1.1
 * @date Nov 9, 2013
 */
public class FCFSSchedulingAlgorithmExample extends DataAwareSchedulingAlgorithmExample {

    ////////////////////////// STATIC METHODS 静态方法///////////////////////
    /**
     * Creates main() to run this example This example has only one datacenter
     * and one storage
	 * 创建main()方法来运行这个例子。本例中只有一个数据中心 
	 * 和一个存储空间
     */
    public static void main(String[] args) {

        try {
            // First step: Initialize the WorkflowSim package. 
			//第一步:初始化WorkflowSim包
            /**
             * However, the exact number of vms may not necessarily be vmNum If
             * the data center or the host doesn't have sufficient resources the
             * exact vmNum would be smaller than that. Take care.
			 * 然而,虚拟机的具体数量可能不一定是vmNum如果 
			 * 数据中心或主机没有足够的资源 
			 * 具体vmNum将小于设定值。注意。
             */
            int vmNum = 5;//number of vms 虚拟机数量;
            /**
             * Should change this based on real physical path
			 * 应该基于当前路径改变path
			 * 更改后采用了相对路径，原码作者采用的是绝对路径，见注释处
             */
            //String daxPath = "/Users/weiweich/NetBeansProjects/WorkflowSim-1.0/config/dax/Montage_100.xml";
			//更改后采用相对路径
            String daxPath = "config/dax/Montage_100.xml";
            File daxFile = new File(daxPath);
            if (!daxFile.exists()) {
                Log.printLine("Warning: Please replace daxPath with the physical path in your working environment!");
                return;
            }

            /**
             * Since we are using HEFT planning algorithm, the scheduling
             * algorithm should be static such that the scheduler would not
             * override the result of the planner
			 * 由于我们使用HEFT规划算法、调度 
			 * 算法应该是静态的,调度器将不会 
			 * 覆盖规划的结果
             */
            Parameters.SchedulingAlgorithm sch_method = Parameters.SchedulingAlgorithm.FCFS;
            Parameters.PlanningAlgorithm pln_method = Parameters.PlanningAlgorithm.INVALID;
            ReplicaCatalog.FileSystem file_system = ReplicaCatalog.FileSystem.LOCAL;

            /**
             * No overheads
			 * 无开销
             */
            OverheadParameters op = new OverheadParameters(0, null, null, null, null, 0);

            /**
             * No Clustering
			 * 没有聚类
             */
            ClusteringParameters.ClusteringMethod method = ClusteringParameters.ClusteringMethod.NONE;
            ClusteringParameters cp = new ClusteringParameters(0, 0, method, null);

            /**
             * Initialize static parameters
			 * 初始化静态参数
             */
            Parameters.init(vmNum, daxPath, null,
                    null, op, cp, sch_method, pln_method,
                    null, 0);
            ReplicaCatalog.init(file_system);

            // before creating any entities.
			//在创建任何实例之前
            int num_user = 1;   // number of grid users网格用户的数量
            Calendar calendar = Calendar.getInstance();
            boolean trace_flag = false;  // mean trace events意思是跟踪事件

            // Initialize the CloudSim library
			//初始化CloudSim库
            CloudSim.init(num_user, calendar, trace_flag);

            WorkflowDatacenter datacenter0 = createDatacenter("Datacenter_0");

            /**
             * Create a WorkflowPlanner with one schedulers.
			 * 创建一个只有一个调度器的工作流调度器
             */
            WorkflowPlanner wfPlanner = new WorkflowPlanner("planner_0", 1);
            /**
             * Create a WorkflowEngine.
			 * 创建一个WorkflowEngine（工作流引擎）
             */
            WorkflowEngine wfEngine = wfPlanner.getWorkflowEngine();
            /**
             * Create a list of VMs.The userId of a vm is basically the id of
             * the scheduler that controls this vm.
			 *创建一个虚拟机列表。一个vm的userId是基本的id 
			 *这个虚拟机调度程序控制。
             */
            List<CondorVM> vmlist0 = createVM(wfEngine.getSchedulerId(0), Parameters.getVmNum());

            /**
             * Submits this list of vms to this WorkflowEngine.
			 * 提交这个虚拟机列表给工作流引擎
             */
            wfEngine.submitVmList(vmlist0, 0);

            /**
             * Binds the data centers with the scheduler.
			 * 将数据中心绑定给调度器
             */
            wfEngine.bindSchedulerDatacenter(datacenter0.getId(), 0);

            CloudSim.startSimulation();
            List<Job> outputList0 = wfEngine.getJobsReceivedList();
            CloudSim.stopSimulation();
            printJobList(outputList0);
        } catch (Exception e) {
            Log.printLine("The simulation has been terminated due to an unexpected error");
        }
    }
}
